<html>
<head>
<title>OneAutumnUtils.ets</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #080808;}
.s1 { color: #e986cd;}
.s2 { color: #653170;}
.s3 { color: #859801;}
.s4 { color: #6c71c4;}
.s5 { color: #ff9170;}
.s6 { color: #870000; font-style: italic;}
.ln { color: #adadad; font-weight: normal; font-style: normal; }
</style>
</head>
<body bgcolor="#ffffff">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#c0c0c0" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
OneAutumnUtils.ets</font>
</center></td></tr></table>
<pre><a name="l1"><span class="ln">1    </span></a><span class="s0">import </span><span class="s1">{ </span><span class="s0">http </span><span class="s1">} </span><span class="s0">from </span><span class="s2">&quot;@kit.NetworkKit&quot;</span><span class="s3">;</span>
<a name="l2"><span class="ln">2    </span></a><span class="s0">import User from </span><span class="s2">&quot;../model/User&quot;</span><span class="s3">;</span>
<a name="l3"><span class="ln">3    </span></a><span class="s0">import Constants from </span><span class="s2">&quot;./Constants&quot;</span><span class="s3">;</span>
<a name="l4"><span class="ln">4    </span></a>
<a name="l5"><span class="ln">5    </span></a><span class="s0">class OneAutumnUtils </span><span class="s1">{</span>
<a name="l6"><span class="ln">6    </span></a>  <span class="s0">getNewIndex</span><span class="s3">&lt;</span><span class="s0">T</span><span class="s3">&gt;</span><span class="s4">(</span><span class="s0">currentIndex</span><span class="s3">:</span><span class="s0">number</span><span class="s3">, </span><span class="s0">array</span><span class="s3">:</span><span class="s0">T</span><span class="s4">[]) </span><span class="s1">{</span>
<a name="l7"><span class="ln">7    </span></a>    <span class="s0">const random </span><span class="s3">= </span><span class="s0">Math</span><span class="s3">.</span><span class="s0">floor</span><span class="s4">(</span><span class="s0">Math</span><span class="s3">.</span><span class="s0">random</span><span class="s4">() </span><span class="s3">* </span><span class="s4">(</span><span class="s0">array</span><span class="s3">.</span><span class="s0">length </span><span class="s3">- </span><span class="s5">1</span><span class="s4">))</span><span class="s3">;</span>
<a name="l8"><span class="ln">8    </span></a>    <span class="s0">return random </span><span class="s3">&gt;= </span><span class="s0">currentIndex </span><span class="s3">? </span><span class="s0">random </span><span class="s3">+ </span><span class="s5">1 </span><span class="s3">: </span><span class="s0">random</span><span class="s3">;</span>
<a name="l9"><span class="ln">9    </span></a>  <span class="s1">}</span>
<a name="l10"><span class="ln">10   </span></a>
<a name="l11"><span class="ln">11   </span></a>  <span class="s6">// 你给我一个数组。我给你从数组中 随机返回一个该数组的索引</span>
<a name="l12"><span class="ln">12   </span></a>  <span class="s0">getRandomIndex</span><span class="s3">&lt;</span><span class="s0">T</span><span class="s3">&gt;</span><span class="s4">(</span><span class="s0">array</span><span class="s3">: </span><span class="s0">T</span><span class="s4">[])</span><span class="s3">: </span><span class="s0">number </span><span class="s1">{</span>
<a name="l13"><span class="ln">13   </span></a>    <span class="s0">if </span><span class="s4">(</span><span class="s0">array</span><span class="s3">.</span><span class="s0">length </span><span class="s3">=== </span><span class="s5">0</span><span class="s4">) </span><span class="s1">{</span>
<a name="l14"><span class="ln">14   </span></a>      <span class="s0">throw new Error</span><span class="s4">(</span><span class="s2">&quot;数组不能为空&quot;</span><span class="s4">)</span><span class="s3">;</span>
<a name="l15"><span class="ln">15   </span></a>    <span class="s1">}</span>
<a name="l16"><span class="ln">16   </span></a>    <span class="s0">return Math</span><span class="s3">.</span><span class="s0">floor</span><span class="s4">(</span><span class="s0">Math</span><span class="s3">.</span><span class="s0">random</span><span class="s4">() </span><span class="s3">* </span><span class="s0">array</span><span class="s3">.</span><span class="s0">length</span><span class="s4">)</span><span class="s3">;</span>
<a name="l17"><span class="ln">17   </span></a>  <span class="s1">}</span>
<a name="l18"><span class="ln">18   </span></a>
<a name="l19"><span class="ln">19   </span></a>  <span class="s6">// 获取随机的单句诗词</span>
<a name="l20"><span class="ln">20   </span></a>  <span class="s0">async getPoemText</span><span class="s4">() </span><span class="s1">{</span>
<a name="l21"><span class="ln">21   </span></a>    <span class="s0">let res </span><span class="s3">= </span><span class="s0">await http</span><span class="s3">.</span><span class="s0">createHttp</span><span class="s4">()</span><span class="s3">.</span><span class="s0">request</span><span class="s4">(</span><span class="s2">'https://v1.jinrishici.com/all.txt'</span><span class="s4">)</span>
<a name="l22"><span class="ln">22   </span></a>    <span class="s0">return res</span><span class="s3">.</span><span class="s0">result</span>
<a name="l23"><span class="ln">23   </span></a>  <span class="s1">}</span>
<a name="l24"><span class="ln">24   </span></a>
<a name="l25"><span class="ln">25   </span></a>  <span class="s6">// 给我一个字符串 处理后返回诗人的名字</span>
<a name="l26"><span class="ln">26   </span></a>  <span class="s0">poetNameToName</span><span class="s4">(</span><span class="s0">poetName</span><span class="s3">: </span><span class="s0">string</span><span class="s4">) </span><span class="s1">{</span>
<a name="l27"><span class="ln">27   </span></a>    <span class="s0">return poetName</span><span class="s3">.</span><span class="s0">split</span><span class="s4">(</span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">separator</span><span class="s4">)[</span><span class="s5">1</span><span class="s4">]</span>
<a name="l28"><span class="ln">28   </span></a>  <span class="s1">}</span>
<a name="l29"><span class="ln">29   </span></a>
<a name="l30"><span class="ln">30   </span></a>  <span class="s6">// 给我一个表示朝代的字符串 如果有朝代数据 就拼接 [] 如果没有就返回空</span>
<a name="l31"><span class="ln">31   </span></a>  <span class="s0">formatDynasty</span><span class="s4">(</span><span class="s0">dynasty</span><span class="s3">: </span><span class="s0">string</span><span class="s4">) </span><span class="s1">{</span>
<a name="l32"><span class="ln">32   </span></a>    <span class="s0">return dynasty</span><span class="s3">.</span><span class="s0">length </span><span class="s3">=== </span><span class="s5">0 </span><span class="s3">? </span><span class="s2">'' </span><span class="s3">: </span><span class="s2">'[' </span><span class="s3">+ </span><span class="s0">dynasty </span><span class="s3">+ </span><span class="s2">']'</span>
<a name="l33"><span class="ln">33   </span></a>  <span class="s1">}</span>
<a name="l34"><span class="ln">34   </span></a>
<a name="l35"><span class="ln">35   </span></a>  <span class="s6">// 校验是否为合法手机号</span>
<a name="l36"><span class="ln">36   </span></a>  <span class="s0">isValidPhoneNumber</span><span class="s4">(</span><span class="s0">phoneNumber</span><span class="s3">: </span><span class="s0">string</span><span class="s4">)</span><span class="s3">: </span><span class="s0">boolean </span><span class="s1">{</span>
<a name="l37"><span class="ln">37   </span></a>    <span class="s6">// 定义手机号的正则表达式</span>
<a name="l38"><span class="ln">38   </span></a>    <span class="s0">const phoneRegex </span><span class="s3">= </span><span class="s2">/^1[3456789]\d{9}$/</span><span class="s3">;</span>
<a name="l39"><span class="ln">39   </span></a>
<a name="l40"><span class="ln">40   </span></a>    <span class="s6">// 使用正则表达式进行校验</span>
<a name="l41"><span class="ln">41   </span></a>    <span class="s0">return phoneRegex</span><span class="s3">.</span><span class="s0">test</span><span class="s4">(</span><span class="s0">phoneNumber</span><span class="s4">)</span><span class="s3">;</span>
<a name="l42"><span class="ln">42   </span></a>  <span class="s1">}</span>
<a name="l43"><span class="ln">43   </span></a>
<a name="l44"><span class="ln">44   </span></a>  <span class="s6">// 校验用户名密码长度是否正确</span>
<a name="l45"><span class="ln">45   </span></a>  <span class="s0">isvlidUser</span><span class="s4">(</span><span class="s0">user</span><span class="s3">: </span><span class="s0">User</span><span class="s4">)</span><span class="s3">: </span><span class="s0">boolean </span><span class="s1">{</span>
<a name="l46"><span class="ln">46   </span></a>    <span class="s0">return user</span><span class="s3">.</span><span class="s0">userName</span><span class="s3">.</span><span class="s0">length </span><span class="s3">&gt;= </span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">usernameLength </span><span class="s3">&amp;&amp; </span><span class="s0">user</span><span class="s3">.</span><span class="s0">password</span><span class="s3">.</span><span class="s0">length </span><span class="s3">&gt;= </span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">passwordLength</span>
<a name="l47"><span class="ln">47   </span></a>  <span class="s1">}</span>
<a name="l48"><span class="ln">48   </span></a>
<a name="l49"><span class="ln">49   </span></a>  <span class="s6">// 判断用户是否登录</span>
<a name="l50"><span class="ln">50   </span></a>  <span class="s0">isLogin</span><span class="s4">(</span><span class="s0">currentUser</span><span class="s3">: </span><span class="s0">User</span><span class="s4">) </span><span class="s1">{</span>
<a name="l51"><span class="ln">51   </span></a>    <span class="s0">return currentUser</span><span class="s3">.</span><span class="s0">phone </span><span class="s3">!= </span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">defaultPhone </span><span class="s3">&amp;&amp; </span><span class="s0">currentUser</span><span class="s3">.</span><span class="s0">password </span><span class="s3">!= </span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">defaultPassword </span><span class="s3">&amp;&amp;</span>
<a name="l52"><span class="ln">52   </span></a>      <span class="s0">currentUser</span><span class="s3">.</span><span class="s0">userName </span><span class="s3">!= </span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">defaultUserName</span>
<a name="l53"><span class="ln">53   </span></a>  <span class="s1">}</span>
<a name="l54"><span class="ln">54   </span></a><span class="s1">}</span>
<a name="l55"><span class="ln">55   </span></a>
<a name="l56"><span class="ln">56   </span></a><span class="s0">let oneAutumnUtils </span><span class="s3">= </span><span class="s0">new OneAutumnUtils</span><span class="s4">()</span>
<a name="l57"><span class="ln">57   </span></a>
<a name="l58"><span class="ln">58   </span></a><span class="s0">export default oneAutumnUtils</span></pre>
</body>
</html>